<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入数据库
use think\Db;

use think\Request;

class Cate extends Backend {
    //默认配置

    public function __construct() {

        // 继承父类
        parent::__construct();

        // 引入模型
        $this->CateModel = model( 'Post.Cate' );
    }

    // 分类列表主页

    public function index() {
        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'name' ] = [ 'like', "%$keywords%" ];
        }

        // 全部用户的数量
        $cateAll = $this->CateModel->select();

        $cateCount = count( $cateAll );

        // 获取全部用户
        $cateList = $this->CateModel->where( $where )->order( 'id', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        // 获取分页显示
        $cateListstr = $cateList->render();

        $this->assign( [
            'cateList' => $cateList,
            'cateListstr' => $cateListstr,
            'cateCount' => $cateCount,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();
    }

    // 添加分类

    public function add() {
        if ( $this->request->isPost() ) {
            // 获取表单数据
            $params = $this->request->param();

            // 组装配置信息
            $data = [
                'name' => $params[ 'name' ],
                'weigh' => $params[ 'weigh' ],
            ];

            // 配置表插入数据
            $result = $this->CateModel->save( $data );

            if ( $result === false ) {
                $this->error( $this->CateModel->getError() );
                exit;
            } else {
                $this->success( '分类信息添加成功！', 'admin/cate/index' );
                exit;
            }

        }
        return $this->fetch();
    }

    // 修改分类

    public function edit() {

        // 获取url传来的数据
        $cateId = $this->request->param( 'id', 0 );

        // 根据id获取配置信息
        $cateInfo = $this->CateModel->find( $cateId );

        // 渲染到界面
        $this->assign( 'cateInfo', $cateInfo );

        if ( $this->request->isPost() ) {

            // 获取表单数据
            $params = $this->request->param();

            // 组装配置信息
            $data = [
                'id' => $cateId,
                'name' => $params[ 'name' ],
                'weigh' => $params[ 'weigh' ],
            ];

            // 分类表插入数据
            $result = $this->CateModel->isUpdate()->save( $data );

            if ( $result === false ) {
                $this->error( $this->CateModel->getError() );
                exit;
            } else {
                $this->success( '分类信息更新成功！', 'admin/cate/index' );
                exit;
            }
        }

        return $this->fetch();

    }

    /* 软删除分类 */

    public function del() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            // 软删除
            $result = $this->CateModel->where( $where )->delete();

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->UserModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除分类成功';
            }

            return json( $success );
        }
    }

    // 检测输入是否正确

    public function check() {
        $request = [
            'result' => false,
            'msg' => '',
        ];

        // 获取输入的分类名
        $name = $this->request->param( 'name' );

        // 获取全部分类信息
        $cateinList = $this->CateModel->where( [ 'name' => $name ] )->find();

        if ( $cateinList ) {
            $request[ 'result' ] = false;
            $request[ 'msg' ] = '分类名已经存在，请重新输入！';
            echo json_encode( $request, JSON_UNESCAPED_UNICODE );
            exit;
        } else {
            $request[ 'result' ] = true;
            $request[ 'msg' ] = '分类名不存在，可以继续操作！';
            echo json_encode( $request, JSON_UNESCAPED_UNICODE );
            exit;
        }

    }
}
?>